rm(list=ls())


# setting wd:
setwd("C:/Users/nkostyuk3/Dropbox (GaTech)/Dissertation/Diffusion/Organizations/Publications/MoD/JPR_November2022/replication_files")
library(survival)
library(dplyr)
library(countrycode)


######################
# TABLE1: THREATS
######################

# loading data:
load('main_data.RData')



#########################
# Table 1 (main analysis)
#########################


mod_list0 = vector(mode='list',length=4)
mod_list0

form1 <- paste0(
      'Surv(tstart,tstop, ADOPTION)~',
      'weights_DEFENSEmp_short+',
      'weights_DEFENSE_long+',
      '+cluster(ISO_ADOPTING)')
form1
mod1 <- coxph(as.formula(form1), data=data, x=TRUE)
mod1
summary(mod1)
mod_list0[[1]] <- mod1


form2 <- paste0(
  'Surv(tstart,tstop, ADOPTION)~',
  'weights_adversary_short+',
  'weights_adversary_long+',
  'LOG_GDP_PERCAP_sc+',
  'cluster(ISO_ADOPTING)')
form2
mod2 <- coxph(as.formula(form2), data=data, x=TRUE)
mod2
summary(mod2) 
mod_list0[[2]] <- mod2
    
    
form3 <- paste0(
      'Surv(tstart,tstop, ADOPTION)~',
      'weights_DEFENSEmp_short+',
      'weights_DEFENSE_long+',
      'weights_adversary_short+',
      'weights_adversary_long+',
      'LOG_GDP_PERCAP_sc+',
      'cluster(ISO_ADOPTING)')
form3
mod3 <- coxph(as.formula(form3), data=data, x=TRUE)
mod3
summary(mod3) 
mod_list0[[2]] <- mod2
    
    
form4 <- paste0(
      'Surv(tstart,tstop, ADOPTION)~',
      'weights_DEFENSEmp_short+',
      'weights_DEFENSE_long+',
      'weights_adversary_short+',
      'weights_adversary_long+',
      'LOG_GDP_PERCAP_sc+', 
      'DEMOCRACY+NATO+prestige+',
      'cluster(ISO_ADOPTING)')
form4
mod4 <- coxph(as.formula(form4), data=data, x=TRUE)
mod4
summary(mod4) # 0.78
mod_list0[[4]] <- mod4
    

rm(mod_list0)
  



##########################
# Robustness Checks:
##########################


#################
# TABLE 2
#################

tab2 = vector(mode = 'list',length = 4)
names(tab2) = c('no_nato', 'conditional', 'coercion_milexp', 'coercion_import')
tab2

#########################
# 1. Robustness checks: 
# (without NATO)
#########################

# remove NATo countries from data: 

data_wout_nato = data %>% 
  filter(NATO!=1)

  
form5a <- paste0(
    'Surv(tstart,tstop, ADOPTION)~',
    'weights_DEFENSE_long+',
    'weights_adversary_short+',
    'weights_adversary_long+',
    'LOG_GDP_PERCAP_sc+',
    'DEMOCRACY+prestige+',
    'cluster(ISO_ADOPTING)')
form5a
mod5a <- coxph(as.formula(form5a), data=data_wout_nato, x=TRUE)
mod5a
summary(mod5a)
exp(confint(mod5a))

tab2[[1]] = mod5a
tab2


#################################################
# 2. Conditional nature of learning/imitation
#################################################

form5 <- paste0(
    'Surv(tstart,tstop, ADOPTION)~',
    'weights_DEFENSEmp_short+',
    'weights_DEFENSE_long+',
    'weights_adversary_short+',
    'weights_adversary_long+',
    'DEMOCRACY+prestige+NATO+',
    'LOG_INT_USERS_sc+',
    'I(weights_DEFENSE_long*new_intusers)+I(weights_DEFENSEmp_short*new_intusers)+',
    'DEFENSEmp_long_cap+',
    'cluster(ISO_ADOPTING)')
form5
mod5 <- coxph(as.formula(form5), data=data, x=TRUE)
mod5
summary(mod5)

tab2[[2]] = mod5
tab2



########################################
# 4. Coercion as a diffusion mechanism
########################################

allies_ivz = allies_ivz = c('DEFENSEmilexp', 
                            'DEFENSEimp'
)

length(allies_ivz)
i = 1; allies_ivz[i]

mod_list0 = vector(mode='list',length=length(allies_ivz))
names(mod_list0) = allies_ivz
mod_list0

for(i in 1:length(allies_ivz)){
  
  
  form4 <- paste0(
    'Surv(tstart,tstop, ADOPTION)~',
    'weights_',allies_ivz[i],'_short+',
    'weights_adversary_short+',
    'weights_adversary_long+',
    'LOG_GDP_PERCAP_sc+',
    'DEMOCRACY+prestige+NATO+',
    'cluster(ISO_ADOPTING)')
  form4
  mod4 <- coxph(as.formula(form4), data=data, x=TRUE)
  mod4
  summary(mod4)
  mod_list0[[i]] <- mod4
  
  
  cat(allies_ivz[i], '\n')
  
} # for i-loop closing
mod_list0

#saveRDS(mod_list0,file=paste0('Analysis/Output/SurvivalModz/RDS/MoDs_results/New_042132023/coercion.rds'))

tab2[[3]] = mod_list0[[1]]
tab2[[4]] = mod_list0[[2]]
tab2

rm(mod_list0)




##################################################
# Additional Robustness Checks: prevalence of 
# cyber as a military issue over time
##################################################  

form0 <- paste0(
  'Surv(tstart,tstop, ADOPTION)~',
  'weights_DEFENSEmp_short+',
  'weights_DEFENSE_long+',
  'weights_adversary_short+',
  'weights_adversary_long+',
  'LOG_GDP_PERCAP_sc+',
  'DEMOCRACY+prestige+',
  'cluster(ISO_ADOPTING)')
form0
mod0 <- coxph(as.formula(form0), data=data, x=TRUE)
mod0
summary(mod0)


# model without alliances term:    
form1 <- paste0(
  'Surv(tstart,tstop, ADOPTION)~',
  'weights_adversary_short+',
  'weights_adversary_long+',
  'LOG_GDP_PERCAP_sc+',
  'DEMOCRACY+prestige+',
  'cluster(ISO_ADOPTING)')
form1
mod1 <- coxph(as.formula(form1), data=data, x=TRUE)
mod1
summary(mod1)

concordance(mod0, mod1)
mod_ally = mod0
mod_no_ally = mod1
# log-likelihood test: source: https://api.rpubs.com/tomanderson_34/lrt
(A = logLik(mod_ally))
(B = logLik(mod_no_ally))
#  test statistic follows a chi-squared distribution with degrees of freedom equal to the difference in the number of free parameters between the complex model and the nested model.
(teststat <- 2 * (as.numeric(A)-as.numeric(B))) # this should always be positive;
# bigger model should always go first. 
#df = 9-7
(p.val <- pchisq(teststat, df = 2, lower.tail = FALSE))  


#################
# TABLE 3
#################

tab3 = vector(mode = 'list',length = 4)
names(tab3) = c('geogrpahy1', 'geography2', 'glm', 'functional')
tab3

########################################
# Additional Robustness Checks: 
# Alternative network specification
########################################
  
form6 <- paste0(
    'Surv(tstart,tstop, ADOPTION)~',
    'weights_contiguity5mp_short+',
    'weights_contiguity5_long+',
    'weights_adversary_short+',
    'weights_adversary_long+',
    'LOG_GDP_PERCAP_sc+',
    'DEMOCRACY+NATO+prestige+',
    'cluster(ISO_ADOPTING)')
form6
mod6 <- coxph(as.formula(form6), data=data, x=TRUE)
mod6
summary(mod6)

  
form7 <- paste0(
    'Surv(tstart,tstop, ADOPTION)~',
    'weights_DEFENSEmp_short+',
    'weights_DEFENSE_long+',
    'weights_contiguity5mp_short+',
    'weights_contiguity5_long+',
    'weights_adversary_short+',
    'weights_adversary_long+',
    'LOG_GDP_PERCAP_sc+',
    'DEMOCRACY+NATO+prestige+',
    'cluster(ISO_ADOPTING)')
form7
mod7 <- coxph(as.formula(form7), data=data, x=TRUE)
mod7
summary(mod7)

tab3[[1]] = mod1
tab3[[2]] = mod4
tab3


#################################
# Additional Robustness Checks:
# GLM
################################

form8 <- paste0(
      'ADOPTION~',
      'weights_DEFENSEmp_short+',
      'weights_DEFENSE_long+',
      'weights_adversary_short+',
      'weights_adversary_long+',
      'LOG_GDP_PERCAP_sc+',
      'DEMOCRACY+NATO+prestige+',
      'factor(YEAR)')
form8
mod8 <- glm(form8,data=data,family=binomial)
mod8
summary(mod8)
exp(coef(mod8))
exp(confint(mod8))  
tab3[[3]] = mod8

##################################
# Additional Robustness Checks:
# FUNCTIONAL FORM
##################################

form9 <- paste0(
    'Surv(tstart,tstop, ADOPTION)~',
    'asinh(weights_DEFENSEmp_short)+',
    'asinh(weights_DEFENSE_long)+',
    'asinh(weights_adversary_short)+',
    'asinh(weights_adversary_long)+',
    'asinh(LOG_GDP_PERCAP_sc)+',
    'DEMOCRACY+NATO+asinh(prestige)+',
    'cluster(ISO_ADOPTING)')
form9
mod9 <- coxph(as.formula(form9), data=data, x=TRUE)
mod9
summary(mod9)
tab3[[4]] = mod9  
tab3
  

#################################
# Additional Robustness Checks:
# Mediation Analysis
#################################

# imitation:
form10 <- paste0(
  'weights_DEFENSEmp_short~',
  'weights_adversary_short+',
  'weights_adversary_long+',
  'weights_adversary_short+',
  'weights_adversary_long+',
  'LOG_GDP_PERCAP_sc+',
  'DEMOCRACY+NATO+prestige+',
  'factor(YEAR)')
form10
mod10 <- glm(form10,data=data)
mod10
summary(mod10)
exp(coef(mod10))
exp(confint(mod10))  
  
# learning:
form11 <- paste0(
  'weights_DEFENSE_long~',
  'weights_adversary_short+',
  'weights_adversary_long+',
  'LOG_GDP_PERCAP_sc+',
  'DEMOCRACY+NATO+prestige+',
  'factor(YEAR)')
form11
mod11 <- glm(form11,data=data)
mod11 
summary(mod11)
exp(coef(mod11))
exp(confint(mod11))  


#################################
# Additional Robustness Checks:
# Alternative DV
#################################

# loading data:
load('data_moe.RData')

form12 <- paste0(
  'Surv(tstart,tstop, ADOPTION)~',
  'weights_DEFENSE_long+',
  'LOG_GDP_PERCAP_sc+', 
  'DEMOCRACY+NATO+prestige+',
  'cluster(ISO_ADOPTING)')
form12
mod12 <- coxph(as.formula(form12), data=data_moe, x=TRUE)
mod12
summary(mod12) 

  

 
